#!/bin/bash

SCRIPT=$(readlink -f "$0")
BASEDIR=$(dirname "$SCRIPT")
cd $BASEDIR

if [ -f ../cdn.tar.gz ];
then
    rm ../cdn.tar.gz
fi

../build.sh
TIMESTAMP=$(date +%m-%d-%H:%M)
# cp ../cdn.tar.gz ~/Desktop/cdn-$TIMESTAMP.tar.gz

# CASEDIR=$(readlink -f "$BASEDIR/../../case_exm/")
CASEDIR=$(readlink -f "$BASEDIR/../../case_example/")
BINFILE=$(readlink -f "$BASEDIR/../bin/cdn")

cp $BASEDIR/resultTest.cpp.rn $CASEDIR/resultTest.cpp
g++ $CASEDIR/resultTest.cpp -o $CASEDIR/resultTest
rm $CASEDIR/resultTest.cpp
TEST=$(readlink -f "$CASEDIR/resultTest")


function lsdir()
{
    for i in `ls`;do
        if [ -d "$i" ] ;
        then
            cd "./$i"
            pwd
            lsdir
            cd ..
        else
            if [ "${i##*.}"x = "case"x ];
            then
                echo $i
                casefile=$i
                casename=${i%.*}
                # $BINFILE $casefile $casename.topo
                # $TEST $casefile $casename.topo
                $BINFILE $casefile $casename.topo > $casename.dump
                $TEST $casefile $casename.topo > $casename.test
                grep need: $casename.test
                grep result_error $casename.test
                grep used $casename.dump
                grep best $casename.dump
            fi
        fi
    done
}

cd $CASEDIR
lsdir
# grep -r used
# grep -r need:
# grep -r result_error